package com.weifly.egp.service.monitor;

import com.weifly.egp.service.stock.StockPrice;
import com.weifly.egp.service.trade.StockOrderInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by weijianxin on 2018/8/26.
 */
public class StockInfo {

    private String stockName; // 名称
    private String stockCode; // 代码
    private Double basePrice; // 基准价格
    private Double gridPrice; // 网格价差
    private Integer tradeUnit; // 每次交易量
    private boolean creditBuy;  // 融资买入
    private List<Double> gridPriceList = new ArrayList<Double>(); // 预设的网格价格，从高到低

    // 下面信息会动态更新
    private Double newestPrice; // 最新价
    private Long newestPriceUpdateTime; // 最新价更新时间
    private List<StockPrice> priceList = new ArrayList<>(); // 股票历史价格

    private StockOrderInfo buyOrderInfo;  // 买入委托单
    private Long buyOrderUpdateTime; // 买入委托单更新时间

    private StockOrderInfo sellOrderInfo;// 卖出委托单
    private Long sellOrderUpdateTime; // 卖出委托单更新时间

    public String getStockName() {
        return stockName;
    }

    public void setStockName(String stockName) {
        this.stockName = stockName;
    }

    public String getStockCode() {
        return stockCode;
    }

    public void setStockCode(String stockCode) {
        this.stockCode = stockCode;
    }

    public Double getBasePrice() {
        return basePrice;
    }

    public void setBasePrice(Double basePrice) {
        this.basePrice = basePrice;
    }

    public Double getGridPrice() {
        return gridPrice;
    }

    public void setGridPrice(Double gridPrice) {
        this.gridPrice = gridPrice;
    }

    public Integer getTradeUnit() {
        return tradeUnit;
    }

    public void setTradeUnit(Integer tradeUnit) {
        this.tradeUnit = tradeUnit;
    }

    public boolean isCreditBuy() {
        return creditBuy;
    }

    public void setCreditBuy(boolean creditBuy) {
        this.creditBuy = creditBuy;
    }

    public List<Double> getGridPriceList() {
        return gridPriceList;
    }

    public void setGridPriceList(List<Double> gridPriceList) {
        this.gridPriceList = gridPriceList;
    }

    public Double getNewestPrice() {
        return newestPrice;
    }

    public void setNewestPrice(Double newestPrice) {
        this.newestPrice = newestPrice;
    }

    public Long getNewestPriceUpdateTime() {
        return newestPriceUpdateTime;
    }

    public void setNewestPriceUpdateTime(Long newestPriceUpdateTime) {
        this.newestPriceUpdateTime = newestPriceUpdateTime;
    }

    public List<StockPrice> getPriceList() {
        return priceList;
    }

    public void setPriceList(List<StockPrice> priceList) {
        this.priceList = priceList;
    }

    public StockOrderInfo getBuyOrderInfo() {
        return buyOrderInfo;
    }

    public void setBuyOrderInfo(StockOrderInfo buyOrderInfo) {
        this.buyOrderInfo = buyOrderInfo;
    }

    public Long getBuyOrderUpdateTime() {
        return buyOrderUpdateTime;
    }

    public void setBuyOrderUpdateTime(Long buyOrderUpdateTime) {
        this.buyOrderUpdateTime = buyOrderUpdateTime;
    }

    public StockOrderInfo getSellOrderInfo() {
        return sellOrderInfo;
    }

    public void setSellOrderInfo(StockOrderInfo sellOrderInfo) {
        this.sellOrderInfo = sellOrderInfo;
    }

    public Long getSellOrderUpdateTime() {
        return sellOrderUpdateTime;
    }

    public void setSellOrderUpdateTime(Long sellOrderUpdateTime) {
        this.sellOrderUpdateTime = sellOrderUpdateTime;
    }

    /**
     * 获取买入价格
     *
     * @return
     */
    public Double takeBuyPrice(){
        int basePriceIndex = this.takeBasePriceIndex();
        if(basePriceIndex==-1){
            throw new RuntimeException("gridPriceList中查不到basePrice");
        }
        if(basePriceIndex==this.gridPriceList.size()-1){
            throw new RuntimeException("basePrice是最后一个，不能确定买入价格");
        }
        return this.gridPriceList.get(basePriceIndex+1);
    }

    /**
     * 获取卖出价格
     *
     * @return
     */
    public Double takeSellPrice(){
        int basePriceIndex = this.takeBasePriceIndex();
        if(basePriceIndex==-1){
            throw new RuntimeException("gridPriceList中查不到basePrice");
        }
        if(basePriceIndex==0){
            throw new RuntimeException("basePrice是第一个，不能确定卖出价格");
        }
        return this.gridPriceList.get(basePriceIndex-1);
    }

    private int takeBasePriceIndex(){
        if(this.basePrice==null){
            throw new RuntimeException("basePrice为空！");
        }
        if(this.gridPriceList==null || this.gridPriceList.isEmpty()){
            throw new RuntimeException("gridPriceList为空！");
        }

        int basePriceIndex = -1;
        for(int i=0;i<this.gridPriceList.size();i++){
            if(this.basePrice.equals(this.gridPriceList.get(i))){
                basePriceIndex = i;
                break;
            }
        }
        return basePriceIndex;
    }
}
